﻿using LearnMAUI2.Services;
using LearnMAUI2.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnMAUI2
{
    public class ServiceLocator
    {
        //官方的服务定位器接口
        private IServiceProvider _serviceProvider;

        //注册导航服务以便在AppShell中使用
        public IRouteSerivice RouteService => _serviceProvider.GetService<IRouteSerivice>();

        //取出ViewModel，以便页面可以使用
        //用于BindingContext="{Binding xxViewModel, Source={StaticResource ServiceLocator}}"
        public ResultPageViewModel ResultPageViewModel => _serviceProvider.GetService<ResultPageViewModel>();

        public TodayPageViewModel TodayPageViewModel => _serviceProvider.GetService<TodayPageViewModel>();

        public DetailPageViewModelProxy DetailPageViewModelProxy => _serviceProvider.GetService<DetailPageViewModelProxy>();

        public MainPageViewModel MainPageViewModel => _serviceProvider.GetService<MainPageViewModel>();

        public QueryPageViewModel QueryPageViewModel => _serviceProvider.GetService<QueryPageViewModel>();

        public ServiceLocator()
        {
            //创建口袋并装入ViewModel
            var serviceCollection = new ServiceCollection();

            //服务注册
            serviceCollection.AddSingleton<IPoetryStorage, PoetryStorage>();
            serviceCollection.AddSingleton<IPreferenceStorage,PreferenceStorage>();            
            serviceCollection.AddSingleton<ITodayPoetryService, TodayPoetryService>();            
            serviceCollection.AddSingleton<ITodayImageService, BingImageService>();
            serviceCollection.AddSingleton<ITodayImageStorage,TodayImageStorage>();
            serviceCollection.AddSingleton<IAlertService, AlertService>();
            //注册RouteService
            serviceCollection.AddSingleton<IRouteSerivice, RouteService>();
            //注册ContentNavigationService
            serviceCollection.AddSingleton<IContentNavigationService, ContentNavigationService>();


            //页面注册
            serviceCollection.AddSingleton<ResultPageViewModel>();
            serviceCollection.AddSingleton<TodayPageViewModel>();
            serviceCollection.AddSingleton<MainPageViewModel>();
            serviceCollection.AddSingleton<QueryPageViewModel>();
            //注册proxy
            serviceCollection.AddSingleton<DetailPageViewModelProxy>();

            _serviceProvider = serviceCollection.BuildServiceProvider();
        }
    }
}
